package com.chalk.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.common.constant.CommonConstants;
import com.chalk.dao.SysPermissionDao;
import com.chalk.model.SysPermission;
import com.chalk.service.SysPermissionService;
import com.chalk.vo.MenuTree;
import com.chalk.vo.SysPermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.CommonDataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统权限表(SysPermission)表服务实现类
 *
 * @author maxiaoqiang
 * @since 2019-03-28 17:19:38
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionDao, SysPermission> implements SysPermissionService {

    @Autowired
    private SysPermissionDao sysPermissionDao;

    /*自定义接口*/

    /**
     * 根据主键批量(逻辑)删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatchByPrimaryKey(String[] ids) {
        return sysPermissionDao.deleteBatchByPrimaryKey(ids);
    }


    @Override
    public List<SysPermissionVo> selectMenus(Map<Object, Object> params) {
        return sysPermissionDao.selectListVo(params);
    }

    @Override
    public List<MenuTree> selectUserTree(MenuTree params) {
        List<MenuTree> menus = new ArrayList<>();
        List<MenuTree> subTrees = this.sysPermissionDao.getMenusByParentId(params);
        subTrees.forEach(dto -> {
            if (dto.getChildCount() > 0) {
                params.setPermId(dto.getPermId());
                dto.setChildList(this.selectUserTree(params));
            }
            menus.add(dto);
        });
        return menus;
    }

    @Override
    public List <MenuTree> selectUserTree(String roleId) {
        List<MenuTree> menus = new ArrayList<>();
        this.sysPermissionDao.getMenusByRoleId(roleId);
        return menus;
    }

    @Override
    public List<MenuTree> selectMenuMarkTree(MenuTree params) {
        List<MenuTree> menus = new ArrayList<>();
        List<MenuTree> subTrees = this.sysPermissionDao.selectMenuMarkTree(params);
        subTrees.forEach(dto -> {
            if (dto.getChildCount() > 0) {
                params.setPermId(dto.getPermId());
                dto.setChildList(this.selectMenuMarkTree(params));
            }
            menus.add(dto);
        });
        return menus;
    }

    @Override
    public List<MenuTree> selectMenuMarkTreeUser(MenuTree params) {
        List<MenuTree> menus = new ArrayList<>();
        List<MenuTree> subTrees = this.sysPermissionDao.selectMenuMarkTreeUser(params);
        subTrees.forEach(dto -> {
            if (dto.getChildCount() > 0) {
                params.setPermId(dto.getPermId());
                dto.setChildList(this.selectMenuMarkTree(params));
            }
            menus.add(dto);
        });
        return menus;
    }

    /**
     * 查询所有节点
     *
     * @param parentId
     * @return
     */
    @Override
    public List<MenuTree> getTree(String parentId) {
        List<MenuTree> menus = new ArrayList<>();
        List<MenuTree> subTrees = this.sysPermissionDao.getTree(parentId);

        subTrees.forEach(dto -> {
            if (dto.getChildCount() > 0) {
                dto.setChildList(this.getTree(dto.getPermId()));
            }
            menus.add(dto);
        });
        return menus;
    }

    @Override
    public List <MenuTree> getTreeByRoleId(String roleId,String parent_id) {
        List<MenuTree> menus = new ArrayList<>();
        List<MenuTree> subTrees=this.sysPermissionDao.getTreeForRoleId(roleId,parent_id);

        subTrees.forEach(dto -> {
            if (dto.getChildCount() > 0) {
                dto.setChildList(this.getTreeByRoleId(roleId,dto.getPermId()));
            }
            menus.add(dto);
        });
        return menus;
    }
}